package com.xt.controller;

import com.xt.GlobalConstant;
import com.xt.bean.PlatformResult;
import com.xt.bean.User;
import com.xt.service.UserService;
import com.xt.utils.FileUpload;
import com.xt.utils.hdfs.SequenceFileUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.hadoop.conf.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;

/**
 * Created by XT on 2018/5/4.
 */
@RestController
@RequestMapping(value = "/file")
public class FileController {

    @Autowired
    private Configuration conf;

    @Value("${file.path.icon}")
    public String USER_ICON_PATH;

    @Value("${hdfs.file.path.record}")
    public String USER_RECORD_PATH;

    @Autowired
    private UserService us;

    @ApiOperation(value = "用户上传头像", notes = "用户上传头像")
    @RequestMapping(value = "/user/icon/upload", method = RequestMethod.POST)
    public PlatformResult uploadIcon(
            HttpServletRequest request,
            @ApiParam(required = true, name = "file", value = "图片文件") @RequestParam("file") MultipartFile file
    ) throws Exception {
        String userId = request.getParameter("userId");
        User user = us.getUserById(userId);
        user.setIcon(FileUpload.uploadFile(file, request.getParameter("userId"), USER_ICON_PATH));
        us.saveOrUpdateUser(user);
//        HashMap<String, InputStream> map = new HashMap<>();
//        map.put(request.getParameter("userId"),file.getInputStream());
//        SequenceFileUtil.upLoadFile(conf,map,USER_ICON_PATH);

        return PlatformResult.ok(user);
    }

    @ApiOperation(value = "用户上传档案", notes = "用户上传档案")
    @RequestMapping(value = "/user/record/upload", method = RequestMethod.POST)
    public PlatformResult uploadRecord(
            HttpServletRequest request,
            @ApiParam(required = true, name = "file", value = "图片文件") @RequestParam("file") MultipartFile file
    ) throws Exception {
        String userId = request.getParameter("userId");
        User user = us.getUserById(userId);
        String recordFilePath = user.getRecordFilePath();
        //
        user.setRecordFilePath(USER_RECORD_PATH);
        us.saveOrUpdateUser(user);
        HashMap<String, InputStream> map = new HashMap<>();
        map.put(request.getParameter("userId"), file.getInputStream());
        boolean status = SequenceFileUtil.updateSequenceFile(conf, map, USER_RECORD_PATH, StringUtils.isEmpty(recordFilePath));
        if (status) {
            return PlatformResult.ok(user);
        } else {
            return PlatformResult.build(GlobalConstant.CODE_ERROR, "上传档案失败");
        }

    }

}
